package com.sundaying.gooway.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.sundaying.gooway.model.UrlClass;
import com.sundaying.gooway.model.User;
import com.sundaying.gooway.util.CipherUtil;
import com.sundaying.gooway.util.DbTool;

/**
 * <p>
 * 用户管理相关操作
 * </p>
 * 
 * @author liuhj
 * @version 1.0.0.1
 */
public class UserService {
	public boolean verify(User user) {
		boolean result = false;
		Connection conn = DbTool.createConn();
		String sql = "select count(*) count from GOW_USER where USERNAME=? AND PASSWORD=? and state in (1)";
		PreparedStatement ps = DbTool.prepare(conn, sql);
		try {
			ps.setString(1, user.getUserName());
			ps.setString(2, CipherUtil.encodeByMD5(user.getPassword()));
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				int count = rs.getInt("count");
				if (count == 1)
					result = true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DbTool.close(ps);
		DbTool.close(conn);
		return result;
	}

	public List<User> list() {
		List<User> userList = new ArrayList();

		Connection conn = DbTool.createConn();
		String sql = "select * from GOW_USER";
		PreparedStatement ps = DbTool.prepare(conn, sql);
		try {
			ResultSet rs = ps.executeQuery();
			User user = null;
			while (rs.next()) {
				user = new User();
				user.setUserID(rs.getInt("USERID"));
				user.setUserName(rs.getString("USERNAME"));
				user.setPassword(rs.getString("PASSWORD"));
				user.setRoleID(rs.getInt("ROLEID"));
				user.setState(rs.getInt("STATE"));
				userList.add(user);
			}
			System.out.println("查询返回记录数：" + userList.size());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DbTool.close(ps);
		DbTool.close(conn);

		return userList;
	}

	public boolean add(User user) {
		boolean result = false;
		try {
			Connection conn = DbTool.createConn();
			String sql = "insert into GOW_USER(USERNAME, PASSWORD, ROLEID, STATE) VALUES(?, ?, ?, ?)";
			PreparedStatement ps = DbTool.prepare(conn, sql);
			try {
				ps.setString(1, user.getUserName());
				ps.setString(2, CipherUtil.encodeByMD5(user.getPassword()));
				ps.setInt(3, user.getRoleID());
				ps.setInt(4, user.getState());
				ps.executeUpdate();
				System.out.println("添加用户成功：" + user.getUserName());
				result = true;
			} catch (SQLException e) {
				e.printStackTrace();
			}
			DbTool.close(ps);
			DbTool.close(conn);

			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	public boolean update(User user) {
		boolean result = false;
		try {
			Connection conn = DbTool.createConn();
			String sql = "update GOW_USER set PASSWORD=?, ROLEID=?, state=? where USERNAME=?";
			PreparedStatement ps = DbTool.prepare(conn, sql);
			try {
				ps.setString(1, CipherUtil.encodeByMD5(user.getPassword()));
				ps.setInt(2, user.getRoleID());
				ps.setInt(3, user.getState());
				ps.setString(4, user.getUserName());
				ps.executeUpdate();
				System.out.println("已更新用户：" + user.getUserName());
				result = true;
			} catch (SQLException e) {
				e.printStackTrace();
			}
			DbTool.close(ps);
			DbTool.close(conn);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public boolean delete(String userName) {
		boolean result = false;
		try {
			Connection conn = DbTool.createConn();
			String sql = "delete from GOW_USER where USERNAME=?";
			PreparedStatement ps = DbTool.prepare(conn, sql);
			try {
				ps.setString(1, userName);
				ps.executeUpdate();
				System.out.println("已删除用户：" + userName);
				result = true;
			} catch (SQLException e) {
				e.printStackTrace();
			}
			DbTool.close(ps);
			DbTool.close(conn);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
